package top.lixunda.ecommerce.server.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.validate.XumValid;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheStoreUser;
import top.lixunda.ecommerce.server.user.api.controller.IStoreAddressController;
import top.lixunda.ecommerce.server.user.api.controller.IUserAddressController;
import top.lixunda.ecommerce.server.user.api.entity.db.StoreAddressDO;
import top.lixunda.ecommerce.server.user.api.entity.db.UserAddressDO;
import top.lixunda.ecommerce.server.user.api.entity.dto.StoreAddressChgDTO;
import top.lixunda.ecommerce.server.user.api.entity.dto.StoreAddressInsertDTO;
import top.lixunda.ecommerce.server.user.api.entity.dto.UserAddressChgDTO;
import top.lixunda.ecommerce.server.user.api.entity.dto.UserAddressInsertDTO;
import top.lixunda.ecommerce.server.user.service.*;

import java.util.List;

/**
 * @author Xumda
 * @version time:2019/12/14 17:47
 * <p>
 * 商铺用户地址管理
 */
@SuppressWarnings({"FieldCanBeLocal", "unused", "WeakerAccess", "UnusedReturnValue"})
@Slf4j
@RestController
public class StoreAddressController extends BaseController implements IStoreAddressController {

    private final IStoreAddressService storeAddressService;

    @Autowired
    public StoreAddressController(IStoreAddressService storeAddressService) {
        this.storeAddressService = storeAddressService;
    }

    /**
     * @see IUserAddressController#queryDefaultUserAddress()
     */
    @Override
    @GlobalTransactional
    public MessageObject<StoreAddressDO> queryDefaultStoreAddress() {
        CacheStoreUser userDetail = currentStoreUserCacheNotNull();
        StoreAddressDO storeAddress = storeAddressService.getOne(
                new LambdaQueryWrapper<StoreAddressDO>()
                        .eq(StoreAddressDO::getAccount, userDetail.getAccount())
                        .eq(StoreAddressDO::getIsDefault, StoreAddressDO.CODE_DEFAULT_ADDRESS)
                        .eq(StoreAddressDO::getUseable, StoreAddressDO.USEABLE),
                false);
        return getSuccessMessageObject("获取默认地址成功", storeAddress);
    }

    /**
     * @param id 用户地址id
     * @return 用户地址信息
     */
    @Override
    @GlobalTransactional
    public MessageObject<StoreAddressDO> queryAddressById(Long id) {
        if (id == null || id <= 0) {
            return MessageObject.error(StoreAddressDO.class, "id不能为空且必须大于0");
        }
        CacheStoreUser userDetail = currentStoreUserCacheNotNull();
        StoreAddressDO storeAddress = storeAddressService.getOne(
                new LambdaQueryWrapper<StoreAddressDO>()
                        .eq(StoreAddressDO::getId, id)
                        .eq(StoreAddressDO::getAccount, userDetail.getAccount())
                        .eq(StoreAddressDO::getUseable, UserAddressDO.USEABLE));
        return getSuccessMessageObject("获取默认地址成功", storeAddress);
    }

    /**
     * 获取用户所有地址信息
     *
     * @see IUserAddressController#queryAllUserAddress()
     */
    @Override
    @GlobalTransactional
    public MessageObject<List<StoreAddressDO>> queryAllStoreAddress() {
        CacheStoreUser userDetail = currentStoreUserCacheNotNull();
        List<StoreAddressDO> storeAddressList = storeAddressService.list(
                new LambdaQueryWrapper<StoreAddressDO>()
                        .eq(StoreAddressDO::getAccount, userDetail.getAccount())
                        .eq(StoreAddressDO::getUseable, StoreAddressDO.USEABLE));
        return getSuccessMessageObject("获取地址成功", storeAddressList);
    }

    /**
     * @see IUserAddressController#insertUserAddress(UserAddressInsertDTO userAddressInsertDTO)
     */
    @Override
    @GlobalTransactional
    public MessageObject<StoreAddressDO> insertStoreAddress(@XumValid StoreAddressInsertDTO storeAddressInsertDTO) {
        CacheStoreUser userDetail = currentStoreUserCacheNotNull();
        StoreAddressDO userAddress = storeAddressInsertDTO.toStoreAddress();
        // 默认地址，将其他地址设置为非默认地址
        if (StoreAddressDO.CODE_DEFAULT_ADDRESS.equals(userAddress.getIsDefault())) {
            storeAddressService.update(new LambdaUpdateWrapper<StoreAddressDO>()
                    .eq(StoreAddressDO::getAccount, userDetail.getAccount())
                    .set(StoreAddressDO::getIsDefault, StoreAddressDO.CODE_NOT_DEFAULT_ADDRESS));
        }
        userAddress.setAccount(userDetail.getAccount());
        boolean success = storeAddressService.save(userAddress);
        return success ? getSuccessMessageObject("保存地址成功", userAddress) :
                getErrorMessageObject("保存地址失败");
    }

    /**
     * @see IUserAddressController#updateUserAddressById(UserAddressChgDTO userAddressChgDTO)
     */
    @Override
    @GlobalTransactional
    public MessageObject<StoreAddressDO> updateStoreAddressById(@XumValid StoreAddressChgDTO storeAddressChgDTO) {
        CacheStoreUser userDetail = currentStoreUserCacheNotNull();
        StoreAddressDO storeAddress = storeAddressChgDTO.toStoreAddress();
        // 默认地址，将其他地址设置为非默认地址
        if (UserAddressDO.CODE_DEFAULT_ADDRESS.equals(storeAddress.getIsDefault())) {
            storeAddressService.update(new LambdaUpdateWrapper<StoreAddressDO>()
                    .eq(StoreAddressDO::getAccount, userDetail.getAccount())
                    .set(StoreAddressDO::getIsDefault, StoreAddressDO.CODE_NOT_DEFAULT_ADDRESS));
        }
        storeAddress.setAccount(userDetail.getAccount());
        boolean success = storeAddressService.updateById(storeAddress);
        return success ? getSuccessMessageObject("更新地址成功", storeAddress) :
                getErrorMessageObject("更新地址失败");
    }

    /**
     * @see IUserAddressController#deleteUserAddressById(Long id)
     */
    @Override
    @GlobalTransactional
    public MessageObject<Boolean> deleteStoreAddressById(Long id) {
        if (id == null || id <= 0) {
            return MessageObject.error("id不能为空且必须大于0", false);
        }
        CacheStoreUser userDetail = currentStoreUserCacheNotNull();
        return storeAddressService.remove(
                new LambdaUpdateWrapper<StoreAddressDO>()
                        .eq(StoreAddressDO::getId, id)
                        .eq(StoreAddressDO::getAccount, userDetail.getAccount()))
                ? getSuccessMessageObject("删除地址信息成功") :
                getErrorMessageObject("删除地址信息失败");
    }
}
